package com.tulingxueyuan.mall.modules.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tulingxueyuan.mall.modules.pms.model.PmsSkuStock;
import com.tulingxueyuan.mall.modules.pms.mapper.PmsSkuStockMapper;
import com.tulingxueyuan.mall.modules.pms.service.PmsSkuStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * sku的库存 服务实现类
 * </p>
 *
 * @author XuShu
 * @since 2023-05-08
 */
@Service
public class PmsSkuStockServiceImpl extends ServiceImpl<PmsSkuStockMapper, PmsSkuStock> implements PmsSkuStockService {

    /**
     * 根据商品id查询sky
     * @param productId
     * @return
     */
    @Override
    public List<PmsSkuStock> listByProductId(Long productId) {
        LambdaQueryWrapper<PmsSkuStock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsSkuStock::getProductId,productId);
        List<PmsSkuStock> list = this.list(queryWrapper);
        return list;
    }

    /**
     * 修改sku
     * @param productId
     * @param list
     * @return
     */
    @Override
    @Transactional
    public boolean update(Long productId, List<PmsSkuStock> list) {
//        先删后加
        LambdaUpdateWrapper<PmsSkuStock> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(PmsSkuStock::getProductId,productId);
        this.remove(wrapper);
        boolean b = this.saveBatch(list);
        return b;
    }

    /**
     * 根据productId，keyword查询sku
     * @param productId
     * @param keyword
     * @return
     */
    @Override
    public PmsSkuStock getByCode(Long productId, String keyword) {
        LambdaQueryWrapper<PmsSkuStock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsSkuStock::getProductId,productId)
                .eq(PmsSkuStock::getSkuCode,keyword);
        PmsSkuStock one = this.getOne(queryWrapper);
        return one;
    }
}
